package controllers;

import beans.ProfileInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import services.impl.ProfileServiceImpl;


import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/profile")
public class ProfileController {
    @Autowired
    private ProfileServiceImpl psi;

    @RequestMapping("/profile")
    public List profile() {
        List<ProfileInfo> list = psi.selectList();
        System.out.println(list);
        return list;
    }

    @RequestMapping("/createprofile")
    public String createProfile(@RequestBody ProfileInfo profileInfo){
        psi.insertSelective(profileInfo);
        System.out.println(profileInfo.toString());
        return null;
    }

    @RequestMapping("/editprofile")
    public String editProfile(@RequestBody ProfileInfo profileInfo) {
        psi.updateByPrimaryKeySelective(profileInfo);
        System.out.println(profileInfo.toString());
        return null;
    }

    @RequestMapping("/detailsprofile")
    public ProfileInfo detailsProfile(HttpServletRequest request) {
        int i = Integer.parseInt(request.getParameter("profileId"));
        ProfileInfo profileInfo = psi.selectByPrimaryKey(i);
        System.out.println(profileInfo);
        return profileInfo;
    }

    @RequestMapping("/searchprofile")
    public List searchProfile(@RequestBody Map<String,String> request) {
        String select = request.get("selectText");
        String input = request.get("inputText");
        List<ProfileInfo> list = psi.selectSelective(select,input);
        return list;
    }

    @RequestMapping("/deleteprofile")
    public String deleteProfile(HttpServletRequest request) {
        int i = Integer.parseInt(request.getParameter("profileId"));
        psi.deleteByPrimaryKey(i);
        System.out.println("删除成功");
        return null;
    }

}
